import axios from "axios";
import { Message } from "element-ui";
import router from '@/router/index'
import { localGet } from "./index";

// 在axios里面配置路径
axios.defaults.baseURL = 'http://localhost:8888';
// 携带cookie，目前项目是token权限
axios.defaults.withCredentials = false
//请求头，headers信息
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers['token'] = localGet('token') || ''
// 默认post请求，使用application/json形式
axios.defaults.headers.post['Content-Type'] = 'application/json'


axios.interceptors.response.use(
    res =>{
        if(typeof res.data !== 'object'){
            Message.error('服务器超时，请重试')
            return Promise.reject(res)
        }
        if(res.status !== 200){
            if(res.data.message) Message.error(res.data.message)
            if(res.status == 302) {
                router.push({path:'/login'})
            }
            return Promise.reject(res.data)
        }
        return res
    })
export default axios